<template>
  <canvas ref="canvasDom" id="canvasDom" class="canvasDom"></canvas>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue';

export default defineComponent({
  name: 'StarryBackground',
  setup() {
    const canvasDom = ref<HTMLCanvasElement | null>(null);

    onMounted(() => {
      const initStarry = () => {
        const canvasDom: HTMLCanvasElement = document.getElementById('canvasDom') as HTMLCanvasElement;
        const canvasCtx: CanvasRenderingContext2D = canvasDom.getContext('2d') as CanvasRenderingContext2D;
        canvasDom.width = window.innerWidth;
        canvasDom.height = window.innerHeight;
        let w = window.innerWidth;
        let h = window.innerHeight;
        var hue = 50; // Hue for yellow
        var stars: any = [];
        var count = 0;
        var maxStars = 300; // 星星数量

        // 第二个画布
        const canvasDom2: HTMLCanvasElement = document.createElement('canvas');
        const canvasCtx2 = canvasDom2.getContext('2d') as CanvasRenderingContext2D;
        canvasDom2.width = 100;
        canvasDom2.height = 100;

        var half = canvasDom2.width / 2; //星星的大小
        var gradient2 = canvasCtx2.createRadialGradient(half, half, 0, half, half, half);
        gradient2.addColorStop(0.025, '#FFFF00'); // 更亮的黄色
        gradient2.addColorStop(0.1, 'hsl(' + hue + ', 100%, 70%)'); // 更亮的中黄色
        gradient2.addColorStop(0.25, 'hsl(' + hue + ', 100%, 40%)'); // 更亮的深黄色
        gradient2.addColorStop(1, 'transparent');
        canvasCtx2.fillStyle = gradient2;
        canvasCtx2.beginPath();
        canvasCtx2.arc(half, half, half, 0, Math.PI * 2); //一个个流星的点
        canvasCtx2.fill();

        function random(min: number, max?: number): number {
          if (arguments.length < 2) {
            max = min;
            min = 0;
          }
          if (max) {
            if (min > max) {
              var hold = max;
              max = min;
              min = hold;
            }
          }

          return Math.floor(Math.random() * ((max as number) - min + 1)) + min;
        }

        function maxOrbit(x: number, y: number) {
          var max = Math.max(x, y);
          var diameter = Math.round(Math.sqrt(max * max + max * max));
          return diameter / 2;
        }

        class Star {
          orbitRadius: number;
          radius: number;
          orbitX: number;
          orbitY: number;
          timePassed: number;
          speed: number;
          alpha: number;

          constructor() {
            this.orbitRadius = random(maxOrbit(w, h));
            this.radius = random(60, this.orbitRadius) / 20;

            this.orbitX = w / 2;
            this.orbitY = h / 2;
            this.timePassed = random(0, maxStars);
            this.speed = random(this.orbitRadius) / 50000;
            this.alpha = random(6, 10) / 10; // 增加星星的亮度
            count++;
            stars[count] = this;
          }

          draw() {
            var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX;
            var y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY;
            var twinkle = random(10);

            if (twinkle === 1 && this.alpha > 0) {
              this.alpha -= 0.05;
            } else if (twinkle === 2 && this.alpha < 1) {
              this.alpha += 0.05;
            }

            canvasCtx.globalAlpha = this.alpha;
            canvasCtx.drawImage(canvasDom2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);

            this.timePassed += this.speed;
          }
        }

        for (var i = 0; i < maxStars; i++) {
          new Star();
        }

        function animation() {
          canvasCtx.globalCompositeOperation = 'source-over';
          canvasCtx.globalAlpha = 0.5;
          canvasCtx.fillStyle = 'hsla(0, 0%, 95%, 1)'; // 更偏白的浅灰色
          canvasCtx.fillRect(0, 0, w, h);

          canvasCtx.globalCompositeOperation = 'lighter';
          for (var i = 1, l = stars.length; i < l; i++) {
            stars[i].draw();
          }
          window.requestAnimationFrame(animation);
        }

        animation();
      };

      initStarry();
    });

    return {};
  }
});
</script>

<style scoped>
.canvasDom {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
}
</style>
